<template>
  <el-form class="b-create-password form"
           ref="form"
           :model="resetForm"
           :rules="rules"
           @keyup.enter.native="reset"
           autocomplete="off">
    <div class="b-create-password__body">
      <el-row>
        <el-col :span="24">
          <el-form-item label="New password" prop="newPassword">
            <el-input v-model.trim="resetForm.newPassword"
                      type="password"
                      autocomplete="new-password"
                      show-password></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="Confirm password"  prop="confirmPassword">
            <el-input v-model.trim="resetForm.confirmPassword"
                      type="password"
                      autocomplete="new-password"
                      show-password></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </div>

    <div class="flex
         justify-end
         b-create-password__footer">
      <el-button size="small"
                 @click="cancel">Cancel</el-button>
      <el-button size="small"
                 type="primary"
                 :loading="loading"
                 @click="reset">Confirm</el-button>
    </div>
  </el-form>
</template>

<script>
export default {
  name: 'BCreatePassword',
  props: {
    form: Object
  },
  data () {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.resetForm.newPassword) {
        callback(new Error(this.$t('custom.bots.design.passwordsNotMatch')))
      } else {
        callback()
      }
    }
    return {
      resetForm: this.form ? Object.assign({}, this.form, {
        confirmPassword: undefined,
        newPassword: undefined
      }) : {},
      rules: {
        newPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async reset () {
      try {
        this.loading = true
        const value = await this.$refs['form'].validate()
        if (value) {
          const { resetClinentPortalPassword } = this.$interface.system
          const params = {
            ...this.resetForm
          }
          const { data: response } = await resetClinentPortalPassword(params)
          if (response.code !== 0) {
            throw response.msg
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.$emit('success')
            }
          })
        }
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    cancel () {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang="scss" scoped>
.b-create-password {
  .el-form-item {
    margin-bottom: 16px;

    &.password {
      line-height: 1;
      ::v-deep .el-form-item__label {
        display: flex;
        justify-content: space-between;
        width: 100%;
        .el-button {
          padding: 0;
        }
      }
    }
    ::v-deep .el-form-item__label {
      font-size: 14px;
      color: #404040;
      font-weight: 600;
      line-height: 14px;
      height: 22px;
      &::before {
        display: none;
      }
    }

    ::v-deep .el-form-item__content {
      .el-input__inner {
        height: 36px;
        line-height: 36px;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px white inset !important;
        }
      }
    }

    .form-btn {
      color: #ffffff;
      border-radius: 2px;
      background: #17b3a3;
    }
  }
  &__body {
    padding: 16px;
  }
  &__footer {
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    padding: 16px;
  }
}
</style>
